<template>
  <div class="wrapper">
  <swiper :options="swiperOption" v-if="showSwiper">
    <!-- slides -->
    <swiper-slide v-for="item in list" :key="item.id">
      <img class="swiper-img" :src="item.carouselImg" @click="imgClick(item)">
    </swiper-slide>
    <!-- Optional controls -->
    <div class="swiper-pagination"  slot="pagination"></div>
  </swiper>
  </div>
</template>

<script>
import {mapMutations} from 'vuex'
export default {
  name: 'HomeSwiper',
  props: {
    list: Array
  },
  data () {
    return {
      swiperOption: {
        pagination: '.swiper-pagination',
        loop: true,
        autoplay: 3000,
        speed:400
      }
    }
  },
  computed: {
    showSwiper () {
      return this.list.length
    }
  },
  methods:{
    ...mapMutations(['changeProduct']),
    imgClick(item){
      switch (item.type){
        case '1':
          this.changeProduct(item);
          this.$router.push({
            name: 'product',
            query: {
              id: item.productId,
              name:item.carouselTitle
            }
          })
          break;
        case '2':
          this.$router.push({
            name: 'creditcardDetail',
            query: {
              id: item.productId,
              name:item.carouselTitle
            }
          })
            break;
        case '3':
          if(item.carouselUrl){
            window.location.href = item.carouselUrl
          }
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  /*子组件样式穿透*/
  .wrapper >>> .swiper-container-horizontal > .swiper-pagination-bullets
    bottom : .32rem
  .wrapper >>> .swiper-pagination-bullet
    background-color :#ffffff
    opacity :.2
  .wrapper >>> .swiper-container
   height 0
   padding-bottom 45.35%
   box-sizing border-box
  .wrapper >>> .swiper-pagination-bullet-active
   background :#ffffff
   opacity 1
  .wrapper
    width 100%
    margin 0 0 .1rem 0
    box-sizing border-box
    .swiper-img
      width 100%
      height 45vw
</style>
